package com.jiuzhe.commercial.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jiuzhe.commercial.http.HttpClient;

import butterknife.ButterKnife;
import butterknife.Unbinder;
/**
 *
 */
public abstract class BaseFragment extends Fragment {
    protected View rootView;
    private Unbinder bind;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(getLayoutResource(), container, false);
        }
       ViewGroup parent = (ViewGroup) rootView.getParent();
       if (parent != null) {
            parent.removeAllViews();
        }
       // container.addView(rootView);
        bind = ButterKnife.bind(this, rootView);
        return rootView;
}

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    protected abstract int getLayoutResource();

    public String getName() {
        return BaseFragment.class.getName();
    }


    protected void changeFragment(Fragment fragment, Bundle args) {
        // 判断传递参数是否为空,如果不为空,设置到fragment上
        if (args != null) {
            fragment.setArguments(args);
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        //取消请求
        HttpClient.cancelRequest(this);
        if (bind != null) {
            bind.unbind();
        }

       if (rootView != null) {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
        }
    }
}
